package com.theme.passport.controller.theme;

import com.theme.common.mvc.utils.PageUtils;
import com.theme.common.mvc.view.R;
import com.theme.passport.entity.theme.WidgeClassifyEntity;
import com.theme.passport.service.theme.WidgeClassifyService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;



/**
 * 组件分类表
 *
 * @author live
 * @email 870459550@qq.com
 * @date 2022-06-21 10:01:48
 */
@RestController
@RequestMapping("widge/widge-classify")
public class WidgeClassifyController {
    @Autowired
    private WidgeClassifyService widgeClassifyService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("widge:widge-classify:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = widgeClassifyService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 列表
     */
    @RequestMapping("/listTree")
    public R listTree() {
        List<WidgeClassifyEntity> classifyEntityList = widgeClassifyService.list();
        for (WidgeClassifyEntity wpClassifyEntity : classifyEntityList) {
            WidgeClassifyEntity parentClassifyEntity = widgeClassifyService.getById(wpClassifyEntity.getParentId());
            if (parentClassifyEntity != null) {
                wpClassifyEntity.setParentName(parentClassifyEntity.getName());
            }
        }
        return R.ok().put("data", classifyEntityList);
    }

    /**
     * 列表
     */
    @RequestMapping("/listParent")
    @RequiresPermissions("widge:widge-classify:list")
    public R listParent(@RequestParam(value = "parentId", required = false) Long parentId,
                        @RequestParam(value = "isParent", required = false) Integer isParent) {
        Map<String, Object> params = new HashMap<>();
        if (parentId != null) {
            params.put("parent_id", parentId);
        }
        if (isParent != null) {
            params.put("is_parent", isParent);
        }
        params.put("page", 1);
        params.put("limit", 100000);
        PageUtils pages = widgeClassifyService.queryPage(params);
        return R.ok().put("data", pages.getList());
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("widge:widge-classify:info")
    public R info(@PathVariable("id") Long id){
		WidgeClassifyEntity widgeClassify = widgeClassifyService.getById(id);

        return R.ok().put("widgeClassify", widgeClassify);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("widge:widge-classify:save")
    public R save(@RequestBody WidgeClassifyEntity widgeClassify){
        widgeClassify.setCreated(new Date());
		widgeClassifyService.save(widgeClassify);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("widge:widge-classify:update")
    public R update(@RequestBody WidgeClassifyEntity widgeClassify){
        widgeClassify.setUpdated(new Date());
		widgeClassifyService.updateById(widgeClassify);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("widge:widge-classify:delete")
    public R delete(@RequestBody Long[] ids){
		widgeClassifyService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
